<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

define('IN_UkCMS', TRUE);
define('UkCMS_VERSION', 'V2.0.0');
define('UkCMS_BUILDTIME', '2011年11月16日 20:01:37');
//CI从2.0.3开始放弃该常量，这里做个兼容
if (!defined('EXT')) {
	define('EXT', '.php');
}

abstract class Uk_Controller extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('uk/platform');
		$this->load->library('uk/settings');
		$this->load->helper('url');
	}
}



//后台控制器
abstract class Backend_Controller extends Uk_Controller {
	function __construct() {
		parent::__construct();
		//设置session参数
		$this->config->set_item('sess_cookie_name', 'uk_session');
		$this->config->set_item('sess_expiration', 7200);
		$this->config->set_item('sess_expire_on_close', FALSE);
		$this->config->set_item('sess_encrypt_cookie', FALSE);
		$this->config->set_item('sess_use_database', TRUE);
		$this->config->set_item('sess_table_name', 'uk_sessions');
		$this->config->set_item('sess_match_ip', FALSE);
		$this->config->set_item('sess_match_useragent', TRUE);
		$this->config->set_item('sess_time_to_update', 300);
		//
		$this->settings->load('backend');
		$this->load->switch_theme('on', setting('backend_theme'), 'admincp/');
		$this->load->library('session');
		$this->load->database();
	}
}


//后台控制器扩展
abstract class Admin_Controller extends Backend_Controller {

	function __construct() {
		parent::__construct();
		date_default_timezone_set('PRC'); //强制时区为PRC,以后可增加配置变量
		$this->_check_login();
	}

	function _check_login() {
		if (!$this->session->userdata('uid')) {
			redirect(setting('backend_access_point') . '/login');
		} else {
			$this->_admin = $this->db->select('uk_admins.uid , uk_admins.username, uk_admins.role , uk_roles.name')
			->from('uk_admins')
			->join('uk_roles', 'uk_roles.id = uk_admins.role')
			->where('uk_admins.uid', $this->session->userdata('uid'))
			->get()
			->row();
		}
	}

	function _template($template, $data = array()) {
		$data['tpl'] = $template;
		$this->load->view('template', $data);
	}

	function _message($msg, $goto = '', $auto = true, $fix = '') {
		if ($goto == '') {
			$goto = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();
		} else {
			$goto = strpos($goto, 'http') !== false ? $goto : backend_url($goto);
		}
		$goto .= $fix;
		$this->_template('sys_message', array('msg' => $msg, 'goto' => $goto, 'auto' => $auto));
		echo $this->output->get_output();
		exit();
	}

}

//后台URI生成函数
function backend_url($uri, $qs = '') {
	return site_url(setting('backend_access_point') . '/' . $uri) . ($qs == '' ? '' : '?' . $qs);
}